		
		
		*********************************************************
		* 		CALCULATING WEIGHTS BASED ON CENSUS 2016
		*********************************************************
		
		****************************************************
		* 1. Get population counts by cells in CENSUS 2016 *	
		****************************************************
		
		* Calculating weights
		use  "data/pumf-98M0001-E-2016-individuals_F1.dta", clear
		
		* Age: 2-levels
		keep if AGEGRP== 11 | AGEGRP== 12 | AGEGRP== 13 | AGEGRP== 14
		recode AGEGRP (11=0) (12=0) (13=1) (14=1), gen(age_2lvl)

		* Province: Keep only people from QC (24) or ONT (35)
		keep if PR==24 | PR==35
		recode PR (24=1) (35=0), gen(pr)

		* Education: 1- HS, 2- Trade Certif and som college, 3- Bachelor and more
		recode HDGREE (1/2=1) (3/8=2) (9/13=3) (else=.), gen(educ_3lvl)
				
		* Recode sex into 0-1 for male -- currently male=2, female=1)
		recode Sex (1=0) (2=1), gen(male)

		egen group= group(age_2lvl pr educ_3lvl male)
		
		* Get the Stats for our descriptive Statistics Table
		
		* Marital Status:
		gen never_married_pct= (MarStH==1) * 100
		gen wid_sep_div_pct= (MarStH==4 | MarStH==5 | MarStH==6) * 100
		gen married_comlaw_pct=(MarStH==2 | MarStH==3) * 100
		
		* Male %
		gen male_pct=male*100
		
		* Education %
		gen educ_hs_less_pct = (educ_3lvl==1)*100
		gen educ_some_uni_pct = (educ_3lvl==2)*100
		gen educ_bach_more_pct = (educ_3lvl==3)*100
		
		* Total Income
		recode TotInc (500000/max=500000)
		gen totinc_i_t = TotInc/1000
		
		* First get a dataset which we can later use to export Descriptive Statistics
		keep WEIGHT group male_pct married_comlaw_pct wid_sep_div_pct never_married_pct educ_hs_less_pct educ_some_uni_pct educ_bach_more_pct totinc_i_t 
		
		save "data/DescStatsCensus2016.dta", replace
		
		* Second get a dataset which we can later use calculate weights
		tabstat group [aw=WEIGHT], stat(sum) by(group) save
		
		mat pop = (r(Stat1)\ r(Stat2)\ r(Stat3)\ r(Stat4)\ r(Stat5)\ r(Stat6)\ r(Stat7)\ r(Stat8)\ r(Stat9)\ r(Stat10)\ r(Stat11)\ r(Stat12)\ r(Stat13)\ r(Stat14)\ r(Stat15)\ r(Stat16)\ ///
				   r(Stat17)\ r(Stat18)\ r(Stat19)\ r(Stat20)\ r(Stat21)\ r(Stat22)\ r(Stat23)\ r(Stat24)\ r(Stat25)\ r(Stat26)\ r(Stat27)\ r(Stat28)\ r(Stat29)\ r(Stat30)\ r(Stat31)\ r(Stat32)\ ///
				   r(Stat33)\ r(Stat34)\ r(Stat35)\ r(Stat36)\ r(Stat37)\ r(Stat38)\ r(Stat39)\ r(Stat40)\ r(Stat41)\ r(Stat42)\ r(Stat43)\ r(Stat44)\ r(Stat45)\ r(Stat46)\ r(Stat47)\ r(Stat48) )
		gen n=_n
		
		svmat pop, name(popcount)
		
		keep if popcount1!=.
		keep popcount1 n 
		rename popcount1 popcount
		rename n group
		
		save "data/popweightsCensus2016.dta", replace
		
		
				
		**************************************************
		* 2. Get population counts by cells in our survey
		**************************************************
		
		use  "data/Clean_data.dta", clear
		
		* 2 levels, 35to44, 45to55.
		gen age_2lvl=age>=45
			
		gen educ_3lvl=educ
		recode educ_3lvl (1=0) (2=0) (3=1) (4=1) (5=1) (6=2) (7=2)
		label def educ_3lvl 0 "HS and less" 1 "Trade Certif/Some college"  2 "University"
		label val educ_3lvl educ_3lvl
		
		egen group= group(age_2lvl province educ_3lvl male)
		
		tabstat group, stat(sum) by(group) save
		
		mat sample = (r(Stat1)\ r(Stat2)\ r(Stat3)\ r(Stat4)\ r(Stat5)\ r(Stat6)\ r(Stat7)\ r(Stat8)\ r(Stat9)\ r(Stat10)\ r(Stat11)\ r(Stat12)\ r(Stat13)\ r(Stat14)\ r(Stat15)\ r(Stat16)\ ///
				   r(Stat17)\ r(Stat18)\ r(Stat19)\ r(Stat20)\ r(Stat21)\ r(Stat22)\ r(Stat23)\ r(Stat24)\ r(Stat25)\ r(Stat26)\ r(Stat27)\ r(Stat28)\ r(Stat29)\ r(Stat30)\ r(Stat31)\ r(Stat32)\ ///
				   r(Stat33)\ r(Stat34)\ r(Stat35)\ r(Stat36)\ r(Stat37)\ r(Stat38)\ r(Stat39)\ r(Stat40)\ r(Stat41)\ r(Stat42)\ r(Stat43)\ r(Stat44)\ r(Stat45)\ r(Stat46)\ r(Stat47)\ r(Stat48) )
		gen n=_n
		
		svmat sample, name(samplecount)
		
		keep if samplecount1!=.
		keep samplecount1 n
		rename samplecount1 samplecount
		rename n group
		
		save "data/sampleweights.dta", replace
		
		
		
		
		************************************************
		* 3. Get Descriptive Statistics in the SFS2016 *
		************************************************
		
		use  "data/SFS-13M0006-E-2016_F1", clear
		
		* Age of main earner
		rename PAGEMIE age
		rename PWEIGHT wgt
		* Gender of main earner
		rename PSEXMIE male
		recode male (1=1) (2=0)
		recode PFTENUR (1/2=1) (3=0), gen(own)
		destring PEDUCMIE, replace
		recode PEDUCMIE (1/2=1) (3=2) (4=3) (else=.), gen(educ)
		gen hs = educ==1 if educ!=.
		gen some = educ==2 if educ!=.
		gen college = educ==3 if educ!=.
		rename PWARRSPL rrsp
		recode rrsp (0=0) (1/max=1), gen(any_rrsp)
		rename  PWATFS tfsa
		recode tfsa (0=0) (1/max=1), gen(any_tfsa)
		recode rrsp (0=.), gen(cond_rrsp)
		recode tfsa (0=.), gen(cond_tfsa)

		gen both = any_rrsp*any_tfsa

		* Market income
		rename PEFMTINC hinc

		keep if age>=35&age<=55

		keep cond_tfsa cond_rrsp wgt


		save "data/DescStatsSFS2016.dta", replace


		
		**********************************************
		* 				LOAD THE DATA
		**********************************************
		
			
		use  "data/Clean_data.dta", clear
		

		* 2 levels, 35to44, 45to55.
		gen age_2lvl=age>=45
		
		gen educ_3lvl=educ
		recode educ_3lvl (1=0) (2=0) (3=1) (4=1) (5=1) (6=2) (7=2)
		label def educ_3lvl 0 "HS and less" 1 "Some College/Uni"  2 "University"
		label val educ_3lvl educ_3lvl
		
		egen group= group(age_2lvl province educ_3lvl male)
		
		label define intervention2 1 "Means-testing arm" 2 "Tax arm" 3 "Control"
		label values intervention intervention2

		
		**********************************************
		* Merge in the preferences calculated by PCM
		**********************************************
		merge 1:1 id using "data/preferences.dta"
		rename _merge _mergeprefs
		
		**********************************************************
		* Merge in the values of I, RI, etc provided in scenarios
		**********************************************************
		merge 1:1 id using "data/final_data_scenarios.dta"
		rename _merge _mergescenarios
		
		* One weird observation with all values missing
		drop if province==.		
		
		*********************************************************
		* Merge in the pop and sample counts to calculate weights
		*********************************************************
		
		merge m:1 group using "data/popweightsCensus2016.dta", keepusing(popcount)
		drop _merge
		
		merge m:1 group using "data/sampleweights.dta", keepusing(samplecount)
		drop _merge 
		
		gen weight=popcount/samplecount
		
				
		*******************/
		* IMPUTING VALUES *
		*******************
		
		mi set wide
		*********************************************************
		*Impute total individual income, given brackets and DK/ R
		*********************************************************
		
		mi register imputed totinc 
		
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if totinc_gt_20k==0 | totinc<=20000, add(1) noi knn(1)
		gen totinc_i = _1_totinc
		
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_20k==1 & totinc_gt_40k==0)  | (totinc>20000 & totinc<=40000), noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_i==. & (totinc_gt_20k==1 & totinc_gt_40k==0)
		
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_40k==1 & totinc_gt_60k==0)  | (totinc>40000 & totinc<=60000), noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_i==. & (totinc_gt_40k==1 & totinc_gt_60k==0)
		
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_60k==1 & totinc_lt_80k==1)  | (totinc>60000 & totinc<=80000), noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_i==. & (totinc_gt_60k==1 & totinc_lt_80k==1)
		
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_lt_80k==0 & totinc_gt_100k==0) | (totinc>80000 & totinc<=100000), noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_i==. & (totinc_lt_80k==0 & totinc_gt_100k==0) 
				
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if totinc_gt_100k==1| totinc>100000 , noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_i==. &totinc_gt_100k==1
		
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all incomes.
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_60k==7 | totinc_gt_60k==8) | totinc!=.,  noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_gt_60k==7 | totinc_gt_60k==8
		
		* If DK/R at LT80k, than impute on 60k+
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_lt_80k==7 | totinc_lt_80k==8) | totinc>=60000 ,  noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_lt_80k==7 | totinc_lt_80k==8
		
		* If DK/R at GT40k, than impute on 60k-
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_40k==7 | totinc_gt_40k==8) | totinc<60000 ,  noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_gt_40k==7 | totinc_gt_40k==8
	
		* If DK/R at GT100k, than impute on 80k+
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_100k==7 | totinc_gt_100k==8) | totinc>=80000 ,  noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_gt_100k==7 | totinc_gt_100k==8
		
		* If DK/R at GT20k, than impute on 40k-
		mi impute pmm totinc i.educ_3lvl c.age##c.age i.male i.province if (totinc_gt_20k==7 | totinc_gt_20k==8) | totinc<40000 ,  noi knn(1) replace
		replace totinc_i = _1_totinc if totinc_gt_20k==7 | totinc_gt_20k==8
	
		*****************************************************
		*Impute total spouse income, given brackets and DK/ R
		*****************************************************
		
		mi register imputed totinc_sp 
		
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if totinc_sp_gt_20k==0 | totinc_sp<=20000, replace noi knn(1)
		gen totinc_sp_i = _1_totinc_sp
		
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_20k==1 & totinc_sp_gt_40k==0)  | (totinc_sp>20000 & totinc_sp<=40000), noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_i==. & (totinc_sp_gt_20k==1 & totinc_sp_gt_40k==0)
		
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_40k==1 & totinc_sp_gt_60k==0)  | (totinc_sp>40000 & totinc_sp<=60000), noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_i==. & (totinc_sp_gt_40k==1 & totinc_sp_gt_60k==0)
		
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_60k==1 & totinc_sp_lt_80k==1)  | (totinc_sp>60000 & totinc_sp<=80000), noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_i==. & (totinc_sp_gt_60k==1 & totinc_sp_lt_80k==1)
		
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_lt_80k==0 & totinc_sp_gt_100k==0) | (totinc_sp>80000 & totinc_sp<=100000), noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_i==. & (totinc_sp_lt_80k==0 & totinc_sp_gt_100k==0) 
				
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if totinc_sp_gt_100k==1| totinc_sp>100000 , noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_i==. &totinc_sp_gt_100k==1
		
	
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all incomes.
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_60k==7 | totinc_sp_gt_60k==8) | totinc_sp!=.,  noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_gt_60k==7 | totinc_sp_gt_60k==8
		
		* If DK/R at LT80k, than impute on 60k+
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_lt_80k==7 | totinc_sp_lt_80k==8) | totinc_sp>=60000 ,  noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_lt_80k==7 | totinc_sp_lt_80k==8
		
		* If DK/R at GT40k, than impute on 60k-
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_40k==7 | totinc_sp_gt_40k==8) | totinc_sp<60000 ,  noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_gt_40k==7 | totinc_sp_gt_40k==8
	
		* If DK/R at GT100k, than impute on 80k+
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_100k==7 | totinc_sp_gt_100k==8) | totinc_sp>=80000 ,  noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_gt_100k==7 | totinc_sp_gt_100k==8
		
		* If DK/R at GT20k, than impute on 40k-
		mi impute pmm totinc_sp i.educ_3lvl c.age##c.age i.male i.province if (totinc_sp_gt_20k==7 | totinc_sp_gt_20k==8) | totinc_sp<40000 ,  noi knn(1) replace
		replace totinc_sp_i = _1_totinc_sp if totinc_sp_gt_20k==7 | totinc_sp_gt_20k==8
	
	
		**********************************************************
		*Impute total household spending, given brackets and DK/ R
		**********************************************************
			
		mi register imputed spend
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  spend_gt_1k==0                     | spend<=1000, noi knn(1) replace
		gen spend_i = _1_spend
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_gt_1k==1 & spend_lt_3k==1)   | (spend>1000 & spend<=3000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_gt_1k==1 & spend_lt_3k==1)
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_lt_3k==0 & spend_gt_5k==0)   | (spend>3000 & spend<=5000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_lt_3k==0 & spend_gt_5k==0)
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_gt_5k==1 & spend_lt_7k==1)   | (spend>5000 & spend<=7000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_gt_5k==1 & spend_lt_7k==1)
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_lt_7k==0 & spend_gt_9k==0)   | (spend>7000 & spend<=9000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_lt_7k==0 & spend_gt_9k==0)
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_gt_9k==1 & spend_gt_11k==0)  | (spend>9000 & spend<=11000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_gt_9k==1 & spend_gt_11k==0)
		 
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_gt_11k==1 & spend_lt_13k==1) | (spend>11000 & spend<=13000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_gt_11k==1 & spend_lt_13k==1)
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if (spend_lt_13k==0 & spend_gt_15k==0) | (spend>13000 & spend<=15000), noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & (spend_lt_13k==0 & spend_gt_15k==0)
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all incomes.
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_gt_9k==7 | spend_gt_9k==8)   | spend!=., noi knn(1) replace
		replace spend_i = _1_spend if  (spend_gt_9k==7 | spend_gt_9k==8)
		
		* If DK/R at GT5k, impute with 9k-
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_gt_5k==7 | spend_gt_5k==8)   | spend<=9000, noi knn(1) replace
		replace spend_i = _1_spend if  (spend_gt_5k==7 | spend_gt_5k==8) 
		
		* If DK/R at LT7k, impute with 5k to 9k
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_lt_7k==7 | spend_lt_7k==8)   | (spend>5000 & spend<=9000), noi knn(1) replace
		replace spend_i = _1_spend if  (spend_lt_7k==7 | spend_lt_7k==8) 
		
		* If DK/R at LT3k, impute with 5k-
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_lt_3k==7 | spend_lt_3k==8)   | (spend<=5000), noi knn(1) replace
		replace spend_i = _1_spend if  (spend_lt_3k==7 | spend_lt_3k==8) 
		
		* If DK/R at GT1k, impute with 3k-
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_gt_1k==7 | spend_gt_1k==8)   | spend<=3000, noi knn(1) replace
		replace spend_i = _1_spend if  (spend_gt_1k==7 | spend_gt_1k==8) 
		
		* If DK/R at LT13k, impute with 9k+
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_lt_13k==7 | spend_lt_13k==8) | (spend>9000), noi knn(1) replace
		replace spend_i = _1_spend if  (spend_lt_13k==7 | spend_lt_13k==8) 
		
		* If DK/R at GT11k, impute with 9k to 13k
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_gt_11k==7 | spend_gt_11k==8) | (spend>9000 & spend<=13000), noi knn(1) replace
		replace spend_i = _1_spend if  (spend_gt_11k==7 | spend_gt_11k==8)
		
		* If DK/R at GT15k, impute with 13k+
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_gt_15k==7 | spend_gt_15k==8) | spend>13000, noi knn(1) replace
		replace spend_i = _1_spend if  (spend_gt_15k==7 | spend_gt_15k==8) 
					
		* If DK/R at LT17k, impute with 15k+
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  (spend_lt_17k==7 | spend_lt_17k==8) | (spend>15000), noi knn(1) replace
		replace spend_i = _1_spend if  (spend_lt_17k==7 | spend_lt_17k==8) 
		
		*******************************************************************************************************************
		**** Only one observation has spending between 15000 and 17000 -- can't impute this way so take a random draw  ****
		*******************************************************************************************************************
						
		replace spend_i = round(15000 + 2000*runiform()) if (spend_gt_15k==1 & spend_lt_17k==1) & spend_i==.
		
		mi impute pmm spend i.educ_3lvl c.age##c.age i.male i.province if  spend_lt_17k==0					   | spend>17000 , noi knn(1) replace
		replace spend_i = _1_spend if spend_i==. & spend_lt_17k==0	

		
		*********************************************************
		*Impute total savings in RRSP, given brackets and DK/ R
		*********************************************************
				
		mi register imputed ind_rrsp_amt
		gen ind_rrsp_amt_i = _1_ind_rrsp_amt
		
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if ind_rrsp_amt_gt10k==2 | ind_rrsp_amt<=10000, noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt
		
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_rrsp_amt_gt10k==1 & ind_rrsp_amt_gt50k==2)  | (ind_rrsp_amt>10000 & ind_rrsp_amt<=50000), noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if ind_rrsp_amt_i==. & (ind_rrsp_amt_gt10k==1 & ind_rrsp_amt_gt50k==2) 
	
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_rrsp_amt_gt50k==1 & ind_rrsp_amt_lt200k==1)  | (ind_rrsp_amt>50000 & ind_rrsp_amt<=200000), noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if ind_rrsp_amt_i==. & (ind_rrsp_amt_gt50k==1 & ind_rrsp_amt_lt200k==1)
				
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if ind_rrsp_amt_lt200k==2| ind_rrsp_amt>200000 , noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if ind_rrsp_amt_i==. & ind_rrsp_amt_lt200k==2
		
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all values
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_rrsp_amt_gt50k==7 | ind_rrsp_amt_gt50k==8) | ind_rrsp_amt!=.,  noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if (ind_rrsp_amt_gt50k==7 | ind_rrsp_amt_gt50k==8)
			
		* If DK/R at LT200k, than impute on 50k+
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_rrsp_amt_lt200k==7 | ind_rrsp_amt_lt200k==8) | ind_rrsp_amt>=50000 ,  noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if (ind_rrsp_amt_lt200k==7 | ind_rrsp_amt_lt200k==8)
	
		* If DK/R at GT10k, than impute on 50k-
		mi impute pmm ind_rrsp_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_rrsp_amt_gt10k==7 | ind_rrsp_amt_gt10k==8) | ind_rrsp_amt<50000 ,  noi knn(1) replace
		replace ind_rrsp_amt_i = _1_ind_rrsp_amt if (ind_rrsp_amt_gt10k==7 | ind_rrsp_amt_gt10k==8)
		
		
		
		*********************************************************
		*Impute total savings in TFSA, given brackets and DK/ R
		*********************************************************
				
		mi register imputed ind_tfsa_amt
		gen ind_tfsa_amt_i = _1_ind_tfsa_amt
		
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if ind_tfsa_amt_gt10k==2 | ind_tfsa_amt<=10000, replace noi knn(1)
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt
		
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_tfsa_amt_gt10k==1 & ind_tfsa_amt_gt50k==2)  | (ind_tfsa_amt>10000 & ind_tfsa_amt<=50000), noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if ind_tfsa_amt_i==. & (ind_tfsa_amt_gt10k==1 & ind_tfsa_amt_gt50k==2) 
	
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_tfsa_amt_gt50k==1 & ind_tfsa_amt_lt200k==1)  | (ind_tfsa_amt>50000 & ind_tfsa_amt<=200000), noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if ind_tfsa_amt_i==. & (ind_tfsa_amt_gt50k==1 & ind_tfsa_amt_lt200k==1)
				
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if ind_tfsa_amt_lt200k==2| ind_tfsa_amt>200000 , noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if ind_tfsa_amt_i==. & ind_tfsa_amt_lt200k==2
		
	
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all values
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_tfsa_amt_gt50k==7 | ind_tfsa_amt_gt50k==8) | ind_tfsa_amt!=.,  noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if (ind_tfsa_amt_gt50k==7 | ind_tfsa_amt_gt50k==8)
			
		* If DK/R at LT200k, than impute on 50k+
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_tfsa_amt_lt200k==7 | ind_tfsa_amt_lt200k==8) | ind_tfsa_amt>=50000 ,  noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if (ind_tfsa_amt_lt200k==7 | ind_tfsa_amt_lt200k==8)
	
		* If DK/R at GT10k, than impute on 50k-
		mi impute pmm ind_tfsa_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_tfsa_amt_gt10k==7 | ind_tfsa_amt_gt10k==8) | ind_tfsa_amt<50000 ,  noi knn(1) replace
		replace ind_tfsa_amt_i = _1_ind_tfsa_amt if (ind_tfsa_amt_gt10k==7 | ind_tfsa_amt_gt10k==8)
		
		
		
		**************************************************************************
		*Impute total savings in non-registered accounts, given brackets and DK/ R
		**************************************************************************
				
		mi register imputed ind_savings_amt
		gen ind_savings_amt_i = _1_ind_savings_amt
		
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if ind_savings_amt_gt10k==2 | ind_savings_amt<=10000, replace noi knn(1)
		replace ind_savings_amt_i = _1_ind_savings_amt
		
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_savings_amt_gt10k==1 & ind_savings_amt_gt50k==2)  | (ind_savings_amt>10000 & ind_savings_amt<=50000), noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if ind_savings_amt_i==. & (ind_savings_amt_gt10k==1 & ind_savings_amt_gt50k==2) 
	
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_savings_amt_gt50k==1 & ind_savings_amt_lt200k==1)  | (ind_savings_amt>50000 & ind_savings_amt<=200000), noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if ind_savings_amt_i==. & (ind_savings_amt_gt50k==1 & ind_savings_amt_lt200k==1)
				
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if ind_savings_amt_lt200k==2| ind_savings_amt>200000 , noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if ind_savings_amt_i==. & ind_savings_amt_lt200k==2
		
		
		
		* Deal with the DK/R
		* If DK/R at first question, impute with all values
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_savings_amt_gt50k==7 | ind_savings_amt_gt50k==8) | ind_savings_amt!=.,  noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if (ind_savings_amt_gt50k==7 | ind_savings_amt_gt50k==8)
			
		* If DK/R at LT200k, than impute on 50k+
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_savings_amt_lt200k==7 | ind_savings_amt_lt200k==8) | ind_savings_amt>=50000 ,  noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if (ind_savings_amt_lt200k==7 | ind_savings_amt_lt200k==8)
	
		* If DK/R at GT10k, than impute on 50k-
		mi impute pmm ind_savings_amt i.educ_3lvl c.age##c.age i.male i.province if (ind_savings_amt_gt10k==7 | ind_savings_amt_gt10k==8) | ind_savings_amt<50000 ,  noi knn(1) replace
		replace ind_savings_amt_i = _1_ind_savings_amt if (ind_savings_amt_gt10k==7 | ind_savings_amt_gt10k==8)
		
		
		* drop the imputed variables here
		
		mi unset
		****************************
		* 		TOTAL SAVINGS 
		****************************
		
		gen totsavings_i=ind_rrsp_amt_i+ind_tfsa_amt_i+ind_savings_amt_i
		
		****************************
		* TOP CODING SOME VARIABLES 
		****************************
		
		sum totinc_i, d
		local max99=r(p99)
		replace totinc_i=`max99' if totinc_i>=`max99' & totinc_i!=.
	
		gen totspend_i= spend_i*12
		sum totspend_i, d
		local max99=r(p99)
		replace totspend_i=`max99' if totspend_i>=`max99' & totspend_i!=.
		
		
		************************************************************************
		* Rename the variables for each scenarios (values given by AskCanadians)
		************************************************************************
		foreach x in 1 2 3 4 5 6 {
			local j = `x'+41
			
			rename Q`j'TblCol1_1 I_`x'
			rename Q`j'TblCol1_2 HHI_`x'
			rename Q`j'TblCol1_3 HHS_`x'
			rename Q`j'TblCol2_1 RI_`x'
			rename Q`j'TblCol2_2 HHRI_`x'
			rename Q`j'TblCol2_3 HHRS_`x'
			rename Q`j'TblCol2_4 GIS_`x'
			rename Q`j'TblCol2_5 OAS_`x'
			
			label variable I_`x' "Income"
			label variable HHI_`x' "Household Income"
			label variable HHS_`x' "Household Spending"
			label variable RI_`x' "Retirement Income"
			label variable HHRI_`x' "Household Ret. Income"
			label variable HHRS_`x' "Household Ret. Spending"
			label variable GIS_`x' "GIS"
			label variable OAS_`x' "OAS"
			
			destring I_`x' HHI_`x' HHS_`x' RI_`x' HHRI_`x' HHRS_`x' GIS_`x' OAS_`x', replace
		}
		
		* Generate the value taken by Y and R
		foreach x in 1 2 3 {
			gen R_`x'=1+0.02
			gen Y_`x'=ValueYQ42
			gen X_`x'=ValueX
		}
		
		foreach x in 4 5 6 {
			gen R_`x'=1+0.05			
			gen Y_`x'=ValueYQ45
			gen X_`x'=ValueX
		}
		
		
		
		* Scaling consumption according to the OECD-Modified Scale 
		* If you are 2 adults in the household, we divide your spending by 1.5
		foreach x in 1 2 3 4 5 6 {
			gen S_`x'=cond(marstat==1|marstat==2, HHS_`x'/1.5, HHS_`x')
			gen RS_`x'=cond(marstat==1|marstat==2, HHRS_`x'/1.5, HHRS_`x')
		}
		
		
		* Generate the number of years between the decision to contribute and the withdrawal
		gen t=70-age
		
		
		
		
		*****************************************************************************
		* 					!! Calculating marginal tax rates !!
		*****************************************************************************
		
		************************************************
		** 1) Income when contributing to RRSP / TFSA **
		************************************************
		
		local l1=13000
		local l2=35000
		local l3=85000
		local l4=104000
		
		* Create income and income + X$ to calculate the average marginal tax rate on the contribution
		foreach x in 1 2 3 4 5 6 {
			gen totinc1_`x'=I_`x'
			gen totinc1_`x'_pX=I_`x'+X_`x'
		}
		
		foreach x in 1 2 3 4 5 6 1_pX 2_pX 3_pX 4_pX 5_pX 6_pX {
			
			gen netinc1_`x'=.
			
			* For people of Ontario
			replace netinc1_`x'=  min(`l1',totinc1_`x')  ///
							+ min((`l2'-`l1') * (1-0.20),(totinc1_`x'-`l1') * (1-0.20)) * cond(totinc1_`x'>=`l1',1,0) ///
							+ min((`l3'-`l2') * (1-0.29),(totinc1_`x'-`l2') * (1-0.29)) * cond(totinc1_`x'>=`l2',1,0) ///
							+ min((`l4'-`l3') * (1-0.41),(totinc1_`x'-`l3') * (1-0.41)) * cond(totinc1_`x'>=`l3',1,0) ///
							+ (totinc1_`x'-`l4') * (1-0.43) * cond(totinc1_`x'>=`l4',1,0) if province==0
			
			* For people of Quebec
			replace netinc1_`x'=  min(`l1',totinc1_`x')  ///
							+ min((`l2'-`l1') * (1-0.24),(totinc1_`x'-`l1') * (1-0.24)) * cond(totinc1_`x'>=`l1',1,0) ///
							+ min((`l3'-`l2') * (1-0.36),(totinc1_`x'-`l2') * (1-0.36)) * cond(totinc1_`x'>=`l2',1,0) ///
							+ min((`l4'-`l3') * (1-0.44),(totinc1_`x'-`l3') * (1-0.44)) * cond(totinc1_`x'>=`l3',1,0) ///
							+ (totinc1_`x'-`l4') * (1-0.47) * cond(totinc1_`x'>=`l4',1,0) if province==1
					
		}	
		
		
		* Generate the marginal tax rate
		foreach x in 1 2 3 4 5 6 {
			gen tau1_`x'=1 - (netinc1_`x'_pX-netinc1_`x') /X_`x'
		}			
					
		*************************************************
		** 2) Income when deducting out of RRSP / TFSA **
		*************************************************	
		
		local l1=13000
		local l2=35000
		local l3=85000
		local l4=104000
		
		* Create income and (income + Y$) to calculate the average marginal tax rate
			
		foreach x in 1 2 3 4 5 6 {
		
			*Income in Case Y is withdrawn from RRSP
			gen totinc2_`x'=RI_`x'
			gen totinc2_`x'_pY=RI_`x'+Y_`x'
		
		}
		
		* When the contribution is in TFSA
		foreach x in 1 2 3 4 5 6 {
					 
			gen inc2_`x'=.
			
			* For people of Ontario
			replace inc2_`x'=     min(`l1',totinc2_`x')  ///
								+ min((`l2'-`l1') * (1-0.24),(totinc2_`x'-`l1') * (1-0.24)) * cond(totinc2_`x'>=`l1',1,0) ///
								+ min((`l3'-`l2') * (1-0.31),(totinc2_`x'-`l2') * (1-0.31)) * cond(totinc2_`x'>=`l2',1,0) ///
								+ min((`l4'-`l3') * (1-0.36),(totinc2_`x'-`l3') * (1-0.36)) * cond(totinc2_`x'>=`l3',1,0) ///
								+ (totinc2_`x'-`l4') * (1-0.40) * cond(totinc2_`x'>=`l4',1,0) if province==0
			
			* For people of Quebec
			replace inc2_`x'=     min(`l1',totinc2_`x')  ///
								+ min((`l2'-`l1') * (1-0.30),(totinc2_`x'-`l1') * (1-0.30)) * cond(totinc2_`x'>=`l1',1,0) ///
								+ min((`l3'-`l2') * (1-0.37),(totinc2_`x'-`l2') * (1-0.37)) * cond(totinc2_`x'>=`l2',1,0) ///
								+ min((`l4'-`l3') * (1-0.39),(totinc2_`x'-`l3') * (1-0.39)) * cond(totinc2_`x'>=`l3',1,0) ///
								+ (totinc2_`x'-`l4') * (1-0.44) * cond(totinc2_`x'>=`l4',1,0) if province==1	
								
			gen netinc2_`x'=inc2_`x'+GIS_`x'+OAS_`x'+Y_`x'
			
		}
		
		local l1=13000
		local l2=35000
		local l3=85000
		local l4=104000
		
		* When the contribution is in RRSP
		foreach x in 1 2 3 4 5 6 {
					 
			gen inc2_`x'_pY=.
			
			* For people of Ontario
			replace inc2_`x'_pY=     min(`l1',totinc2_`x'_pY)  ///
								+ min((`l2'-`l1') * (1-0.24),(totinc2_`x'_pY-`l1') * (1-0.24)) * cond(totinc2_`x'_pY>=`l1',1,0) ///
								+ min((`l3'-`l2') * (1-0.31),(totinc2_`x'_pY-`l2') * (1-0.31)) * cond(totinc2_`x'_pY>=`l2',1,0) ///
								+ min((`l4'-`l3') * (1-0.36),(totinc2_`x'_pY-`l3') * (1-0.36)) * cond(totinc2_`x'_pY>=`l3',1,0) ///
								+ (totinc2_`x'_pY-`l4') * (1-0.40) * cond(totinc2_`x'_pY>=`l4',1,0) if province==0
			
			* For people of Quebec
			replace inc2_`x'_pY=     min(`l1',totinc2_`x'_pY)  ///
								+ min((`l2'-`l1') * (1-0.30),(totinc2_`x'_pY-`l1') * (1-0.30)) * cond(totinc2_`x'_pY>=`l1',1,0) ///
								+ min((`l3'-`l2') * (1-0.37),(totinc2_`x'_pY-`l2') * (1-0.37)) * cond(totinc2_`x'_pY>=`l2',1,0) ///
								+ min((`l4'-`l3') * (1-0.39),(totinc2_`x'_pY-`l3') * (1-0.39)) * cond(totinc2_`x'_pY>=`l3',1,0) ///
								+ (totinc2_`x'_pY-`l4') * (1-0.44) * cond(totinc2_`x'_pY>=`l4',1,0) if province==1	
							
		
			* Deal with the GIS	/ OAS	(25% if married or common law, 50% if single)
				gen netGIS_`x'=GIS_`x' - 0.25  * min(Y_`x', 4*GIS_`x')	if marstat==1 | marstat==2
			replace netGIS_`x'=GIS_`x' - 0.50  * min(Y_`x', 2*GIS_`x')	if marstat!=1 & marstat!=2
			
			* aucun scénario clawback gis et oas -- revoir.
			gen netOAS_`x'=OAS_`x' - 0.15 * min(Y_`x', 1/0.15*OAS_`x')
			
			gen netinc2_`x'_pY=inc2_`x'_pY+netGIS_`x'+netOAS_`x'
		}
					
		
		foreach x in 1 2 3 4 5 6 {
			* Calculate marginal tax rate
			gen tau2_`x'=1 - (netinc2_`x'-netinc2_`x'_pY) /Y_`x'

		}	
		
		
		*****************************
		**** Drop some variables ****
		*****************************
		
		keep  id totinc_i totspend_i status male age province educ marstat age_sp workstatus totinc totinc_sp spend_i own_house marktval_house ind_rrsp ind_tfsa ind_savings ind_rrsp_amt_i ///
		ind_tfsa_amt_i ind_savings_amt_i mortgage rrsp_2017 tfsa_2017 workinc has_rrsp_loan retir_age retir_totinc retir_totinc_sp retir_spend retir_savings finlit_interest ///
		finlit_inflation finlit_risk intervention know_deductible know_withdaw_tax know_returns_tax know_penalty know_room ///
		scn_1 scn_2 scn_3 scn_4 scn_5 scn_6 age_2lvl educ_3lvl group sigma delta I_1 HHI_1 HHS_1 RI_1 HHRI_1 HHRS_1 GIS_1 OAS_1 I_2 HHI_2 ///
		HHS_2 RI_2 HHRI_2 HHRS_2 GIS_2 OAS_2 I_3 HHI_3 HHS_3 RI_3 HHRI_3 HHRS_3 GIS_3 OAS_3 I_4 HHI_4 HHS_4 RI_4 HHRI_4 HHRS_4 GIS_4 OAS_4 I_5 HHI_5 HHS_5 RI_5 HHRI_5 HHRS_5 ///
		GIS_5 OAS_5 I_6 HHI_6 HHS_6 RI_6 HHRI_6 HHRS_6 GIS_6 OAS_6 popcount samplecount weight R_1 Y_1 X_1 R_2 Y_2 X_2 R_3 Y_3 X_3 R_4 Y_4 X_4 R_5 Y_5 X_5 R_6 Y_6 X_6 S_1 RS_1 ///
		S_2 RS_2 S_3 RS_3 S_4 RS_4 S_5 RS_5 S_6 RS_6 t tau1_1 tau1_2 tau1_3 tau1_4 tau1_5 tau1_6 tau2_1 tau2_2 tau2_3 tau2_4 tau2_5 tau2_6
		
		
		* Work with data in long format
				
		reshape long scn_ I_ HHI_ HHS_ RI_ HHRI_ HHRS_ GIS_ OAS_ R_ Y_ X_ S_ RS_ tau1_ tau2_, i(id) j(scn)
		
		rename scn_ scn_choice
		rename I_ I
		rename HHI_ HHI
		rename HHS_ HHS
		rename RI_ RI
		rename HHRI_ HHRI
		rename HHRS_ HHRS
		rename GIS_ GIS
		rename OAS_ OAS
		rename R_ R
		rename Y_ Y
		rename X_ X
		rename S_ S
		rename RS_ RS
		rename tau1_ tau1
		rename tau2_ tau2
		
		* Tag only 1 observation per respondent
		egen tag=tag(id)
		
		* Drop if the person did not see a particular scenario
		drop if S==.
		
		save "data/Final_data.dta", replace
